package com.shanzmoo.pay.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shanzmoo.pay.domain.request.terminalRequest.SaobeiTerminalAddRequest;
import com.shanzmoo.pay.domain.request.terminalRequest.SaobeiTerminalQueryRequest;
import com.shanzmoo.pay.domain.response.terminalResponse.SaobeiTerminalAddResponse;
import com.shanzmoo.pay.domain.response.terminalResponse.SaobeiTerminalQueryResponse;
import com.shanzmoo.pay.util.ApiConstants;
import com.shanzmoo.pay.util.ApiUtil;
import com.shanzmoo.pay.util.HttpClientUtils;
import com.shanzmoo.pay.util.RandomUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author dingmeng
 * @title: TerminalManager
 * @projectName jyf-mall-server
 * @description: 扫呗--终端管理
 * @date 2021/4/415:29
 */
@Slf4j
@Component
public class SaobeiTerminalManager {

    /**
     * 查询终端
     *
     * @param request
     * @return
     * @throws Exception
     */
    @SneakyThrows
    public SaobeiTerminalQueryResponse queryTerminal(SaobeiTerminalQueryRequest request) {
        //发送请求
        log.info("查询终端请求报文:" + JSON.toJSONString(request));
        //参与签名的对象
        SaobeiTerminalQueryRequest signRequest = new SaobeiTerminalQueryRequest();
        signRequest.setInst_no(request.getInst_no());
        signRequest.setTrace_no(request.getTrace_no());
        signRequest.setTerminal_id(request.getTerminal_id());
        JSONObject obj = ApiUtil.convertRequest(request, ApiConstants.key, "key");
        String xmlText = HttpClientUtils.post(ApiConstants.service_url_mch + "/terminal/100/query", obj.toJSONString());
        SaobeiTerminalQueryResponse response = JSON.parseObject(xmlText, SaobeiTerminalQueryResponse.class);
        log.info("查询终端返回报文:" + JSON.toJSONString(response));
        return response;
    }

    /**
     * 创建终端
     *
     * @param request
     * @return
     * @throws Exception
     */
    @SneakyThrows
    public SaobeiTerminalAddResponse addTerminal(SaobeiTerminalAddRequest request) {
        //发送请求
        log.info("创建终端请求报文:" + JSON.toJSONString(request));
        //参与签名的对象
        SaobeiTerminalAddRequest signRequest = new SaobeiTerminalAddRequest();
        signRequest.setInst_no(request.getInst_no());
        signRequest.setMerchant_no(request.getMerchant_no());
        signRequest.setTrace_no(request.getTrace_no());
        JSONObject obj = ApiUtil.convertMerchantRequest(request, signRequest, ApiConstants.key, "key");
        String xmlText = HttpClientUtils.post(ApiConstants.service_url_mch + "/terminal/100/add", obj.toJSONString());
        SaobeiTerminalAddResponse response = JSON.parseObject(xmlText, SaobeiTerminalAddResponse.class);
        log.info("创建终端返回报文:" + JSON.toJSONString(response));
        return response;

    }

//    public static void main(String[] args) throws Exception {
//        //查询终端创建requst
////        SaobeiTerminalQueryRequest requst = new SaobeiTerminalQueryRequest();
////        requst.setInst_no(ApiConstants.INST_NO);
////        requst.setTrace_no(RandomUtils.getRandom(32));
////        requst.setTerminal_id("12467217");
////        queryTerminal(requst);
//        //创建端创建requst
//        SaobeiTerminalAddRequest requst = new SaobeiTerminalAddRequest();
//        requst.setInst_no(ApiConstants.INST_NO);
//        requst.setStore_code(ApiConstants.STORE_CODE);
//        requst.setMerchant_no(ApiConstants.MERCHANT_NO);
//        requst.setTerminal_name("创建测试终端");
//        requst.setTrace_no(RandomUtils.getRandom(32));
//        addTerminal(requst);
//    }

}
